package com.fuershiye.woman.monitor.utils;

import com.fuershiye.woman.monitor.constants.ShowLevel;

public class RuleBaseUtil {

//	private static final DecimalFormat df = new DecimalFormat("######0.00");
//
//	/**
//	 * 获取本年度的增长增率
//	 *
//	 * @param value		今年的实际值
//	 * @param lastYear	去年的实际值
//	 * @return
//	 */
//	public static Double getRate(Double value, Double lastYear) {
//
//		if(value==null){
//			return (double) 0;
//		}
//		if(lastYear==null){
//			return (double) 100;
//		}
//
//		return Double.valueOf(df.format((value - lastYear)/lastYear*100));
//	}

	/**
	 * 是否存在数值，存在即达标
	 *
	 * @param value	实际值
	 * @return
	 */
	public static Integer getExsit(Double value) {

		if(value==null||value==0){
			return ShowLevel.SCORE_10.getCode();
		}
		return ShowLevel.SCORE_50.getCode();
	}

	/**
	 * 实际值和目标值相等才算达标
	 *
	 * @param now	实际值
	 * @param aim	目标值（五年计划目标值）
	 * @return
	 */
	public static Integer getAimEqual(Double now, Double aim) {

		if(now==null || aim==null){
			return null;
		}
		if(aim.equals(now)){
			return ShowLevel.SCORE_50.getCode();
		}
		return ShowLevel.SCORE_10.getCode();
	}

	/**
	 * 目标值是否大于实际值
	 *
	 * @param now	实际值
	 * @param lastYear	去年值
	 * @param last2Year	前年值
//	 * @param equal	是否包括目标值
	 * @return
			SCORE_10(10, "红色"),
			SCORE_20(20, "橙色"),
			SCORE_30(30, "黄色"),
			SCORE_40(40, "浅绿"),
			不会返回(50, "深绿")
	 */
	public static Integer getUp2Year(Double now, Double lastYear, Double last2Year) {

		//当年没数据
		if(now==null){
			return null;
		}

		//当年有数据，去年无数据
		if(lastYear==null){
			return ShowLevel.SCORE_40.getCode();
		}

		//前年无数据，当年、去年有数据
		if(last2Year==null && now>lastYear){
			return ShowLevel.SCORE_40.getCode();
		}
		if(last2Year==null && now.equals(lastYear)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(last2Year==null && now<lastYear){
			return ShowLevel.SCORE_10.getCode();
		}

		//去年 > 前年
		if(last2Year!=null && lastYear>last2Year && now<lastYear && now<last2Year){
			return ShowLevel.SCORE_10.getCode();
		}
		if(last2Year!=null && lastYear>last2Year && now.equals(last2Year)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(last2Year!=null && lastYear>last2Year && now<lastYear && now>last2Year){
			return ShowLevel.SCORE_30.getCode();
		}
		if(last2Year!=null && lastYear>last2Year && now.equals(lastYear)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(last2Year!=null && lastYear>last2Year && now>lastYear){
			return ShowLevel.SCORE_40.getCode();
		}

		//去年 = 前年
		if(lastYear.equals(last2Year) && now>lastYear){
			return ShowLevel.SCORE_40.getCode();
		}
		if(lastYear.equals(last2Year) && now.equals(lastYear)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(lastYear.equals(last2Year) && now<lastYear){
			return ShowLevel.SCORE_10.getCode();
		}

		//去年 < 前年
		if(lastYear<last2Year && now<lastYear && now<last2Year){
			return ShowLevel.SCORE_10.getCode();
		}
		if(lastYear<last2Year && now.equals(lastYear)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(lastYear<last2Year && now>lastYear && now<last2Year){
			return ShowLevel.SCORE_30.getCode();
		}
		if(lastYear<last2Year && now.equals(last2Year)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(lastYear<last2Year && now>last2Year){
			return ShowLevel.SCORE_40.getCode();
		}
		return null;
	}


	/**
	 * 目标值是否大于实际值
	 *
	 * @param now	实际值
	 * @param lastYear	去年值
	 * @param last2Year	前年值
	//	 * @param equal	是否包括目标值
	 * @return
	SCORE_10(10, "红色"),
	SCORE_20(20, "橙色"),
	SCORE_30(30, "黄色"),
	SCORE_40(40, "浅绿"),
	不会返回(50, "深绿")
	 */
	public static Integer getDown2Year(Double now, Double lastYear, Double last2Year) {

		//当年没数据
		if(now==null){
			return null;
		}

		//当年有数据，去年无数据
		if(lastYear==null){
			return ShowLevel.SCORE_40.getCode();
		}

		//前年无数据，当年、去年有数据
		if(last2Year==null && now>lastYear){
			return ShowLevel.SCORE_10.getCode();
		}
		if(last2Year==null && now.equals(lastYear)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(last2Year==null && now<lastYear){
			return ShowLevel.SCORE_40.getCode();
		}

		//去年 > 前年
		if(last2Year!=null && lastYear>last2Year && now<lastYear && now<last2Year){
			return ShowLevel.SCORE_40.getCode();
		}
		if(last2Year!=null && lastYear>last2Year && now.equals(last2Year)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(last2Year!=null && lastYear>last2Year && now<lastYear && now>last2Year){
			return ShowLevel.SCORE_30.getCode();
		}
		if(last2Year!=null && lastYear>last2Year && now.equals(lastYear)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(last2Year!=null && lastYear>last2Year && now>lastYear){
			return ShowLevel.SCORE_10.getCode();
		}

		//去年 = 前年
		if(lastYear.equals(last2Year) && now>lastYear){
			return ShowLevel.SCORE_10.getCode();
		}
		if(lastYear.equals(last2Year) && now.equals(lastYear)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(lastYear.equals(last2Year) && now<lastYear){
			return ShowLevel.SCORE_40.getCode();
		}

		//去年 < 前年
		if(lastYear<last2Year && now<lastYear && now<last2Year){
			return ShowLevel.SCORE_40.getCode();
		}
		if(lastYear<last2Year && now.equals(lastYear)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(lastYear<last2Year && now>lastYear && now<last2Year){
			return ShowLevel.SCORE_30.getCode();
		}
		if(lastYear<last2Year && now.equals(last2Year)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(lastYear<last2Year && now>last2Year){
			return ShowLevel.SCORE_10.getCode();
		}
		return null;
	}

	/**
	 * 实际值大于(等于)目标值
	 *
	 * @param now	实际值
	 * @param base	当年目标值
	 * @param aim	五年计划的目标值
	 * @param equal	五年计划的目标值
	 * @return
	 */
	public static Integer getAimUp(Double now, Double base, Double aim, Boolean equal) {

		if(now==null||base==null||aim==null){
			return null;
		}
		//完成五年计划
		if(now>aim){
			return ShowLevel.SCORE_50.getCode();
		}
		if(now.equals(aim) && equal){
			return ShowLevel.SCORE_50.getCode();
		}
		//完成当年计划
		if(now<=aim && now>=base){
			return ShowLevel.SCORE_40.getCode();
		}
		//当年未完成的
		if(now<base && now>=(base*8/10)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(now<base && now>=(base*6/10)){
			return ShowLevel.SCORE_20.getCode();
		}
		if(now<base && now<(base*6/10)){
			return ShowLevel.SCORE_10.getCode();
		}
		return null;
	}

	/**
	 * 实际值大于(等于)目标值
	 *
	 * @param now	实际值
	 * @param aim	五年计划的目标值
	 * @param equal	五年计划的目标值
	 * @return
	 */
	public static Integer getAimUp(Double now, Double aim, Boolean equal) {

		if(now==null||aim==null){
			return null;
		}
		//完成五年计划
		if(now>aim){
			return ShowLevel.SCORE_50.getCode();
		}
		if(now.equals(aim) && equal){
			return ShowLevel.SCORE_50.getCode();
		}
		//未完成五年计划
		if(now>=(aim*8/10)){
			return ShowLevel.SCORE_40.getCode();
		}
		if(now>=(aim*6/10)){
			return ShowLevel.SCORE_30.getCode();
		}
		if(now<(aim*6/10)){
			return ShowLevel.SCORE_10.getCode();
		}
		return null;
	}


	/**
	 * 实际值小于(等于)目标值
	 *
	 * @param now	实际值
	 * @param base	当年目标值
	 * @param aim	五年计划的目标值
	 *			now		base	aim
	 *			4		8		5		完成
	 *			6		8		5
	 *			10		8		5
	 *
	 * @return
	 */
	public static Integer getAimDown(Double now, Double base, Double aim, Boolean equal) {

		if(now==null||base==null||aim==null){
			return null;
		}
		//完成五年计划
		if(now<aim){
			return ShowLevel.SCORE_50.getCode();
		}
		if(now.equals(aim) && equal){
			return ShowLevel.SCORE_50.getCode();
		}
		//完成当年计划
		if(now>=aim && now<=base){
			return ShowLevel.SCORE_40.getCode();
		}

		if(base==0 && now>0){
			return ShowLevel.SCORE_10.getCode();
		}
		if(base==0 && now<0){
			return ShowLevel.SCORE_50.getCode();
		}

		//当年未完成的
		if(base!=0 && now>base && (now-base)/base<=2/10){
			return ShowLevel.SCORE_30.getCode();
		}
		if(base!=0 && now>base && (now-base)/base<=4/10){
			return ShowLevel.SCORE_20.getCode();
		}
		if(base!=0 && now>base && (now-base)/base>4/10){
			return ShowLevel.SCORE_10.getCode();
		}
		return null;
	}

	/**
	 * 实际值小于(等于)目标值
	 *
	 * @param now	实际值
	 * @param aim	五年计划的目标值
	 *			now		base	aim
	 *			4		8		5		完成
	 *			6		8		5
	 *			10		8		5
	 *
	 * @return
	 */
	public static Integer getAimDown(Double now, Double aim, Boolean equal) {

		if(now==null||aim==null){
			return null;
		}
		//完成五年计划
		if(now<aim){
			return ShowLevel.SCORE_50.getCode();
		}
		if(now.equals(aim) && equal){
			return ShowLevel.SCORE_50.getCode();
		}
		//未完成五年计划
		if((now-aim)/aim<=2/10){
			return ShowLevel.SCORE_40.getCode();
		}
		if((now-aim)/aim<=4/10){
			return ShowLevel.SCORE_30.getCode();
		}
		if((now-aim)/aim>4/10){
			return ShowLevel.SCORE_10.getCode();
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println("here");

//		double now = 100l;
//		double aim = 100l;
//		System.out.println(getUp(now,aim,false));
//		System.out.println(getUp(now,aim,true));
//
//		System.out.println(getDown(now,aim,false));
//		System.out.println(getDown(now,aim,true));

		double now = 0;
		double aim = 0;

//		System.out.println(getUp(now,aim,false));
//		System.out.println(getDown(now,aim,false));

	}

}